﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using MvcEngine.Core;
using MvcEngine.Mvc.Filters;
using System.Web.Routing;

namespace MvcEngine.Mvc.Infrastructure
{
    public class MvcEngineControllerFactory : DefaultControllerFactory
    {
        public override IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
        {
            return base.CreateController(requestContext, controllerName);
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            if (controllerType == null)
                return null;
            //IController iController = base.GetControllerInstance(requestContext, controllerType);
            IController iController = IoC.Resolve(controllerType) as IController;
            if (typeof(Controller).IsAssignableFrom(controllerType))
            {
                Controller controller = iController as Controller;
                if (controller != null)
                    controller.ActionInvoker = IoC.Resolve<IActionInvoker>();
            }
            return iController;
        }
    }
}
